package com.xiyi_design.printer_manage.pub;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PubFunction {
	
	// 获得 随机数 枷锁保护
	public static String getRanNum4Code() {
		Lock lock = new ReentrantLock();
		String num = "";
		try {
			lock.lock();
			num = PubFunction.get4Num();
			lock.unlock();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return num;
	}
	
	// 生成4位随机数
	public static String get4Num() {
		Random random = new Random();
		int code = random.nextInt(9000) + 1000;
		return code+"";
	}
	
	// 生成当前时间
	public static String getNewDate() {
		Date date = new Date();
		java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatdatetime.format(date);
	}

	// 产生全局唯一ID
	public static String makeGUID(){
		UUID uuid = UUID.randomUUID();
	    String id = uuid.toString();
	    id = id.replace("-", "");
	    return id;
	}

 	public static boolean isEmptyString(String str){
		if (str == null || str.equals("")){
			return true;
		}
		return false;
	}

	public static String HTMLEncode(String con) {
		String strCon = con;
		strCon = strCon.replace("&", "&amp;");
		strCon = strCon.replace(">", "&gt;");
		strCon = strCon.replace("<", "&lt;");
		strCon = strCon.replace("\"", "&quot;");
		return strCon;
	}

	public static String HTMLDecode(String con) {
		String strCon = con;
		strCon = strCon.replace("&gt;", ">");
		strCon = strCon.replace("&lt;", "<");
		strCon = strCon.replace("&quot;", "\"");
		strCon = strCon.replace("&amp;", "&");
		return strCon;
	}

	public static boolean IsPcClient(HttpServletRequest request) {
		boolean bIsPc = false;
		 String s1 = request.getHeader("user-agent");
		 if(s1.contains("Android")) {
			 System.out.println("Android移动客户端");
		 } else if(s1.contains("iPhone")) {
			 System.out.println("iPhone移动客户端");
		 }  else if(s1.contains("iPad")) {
			 System.out.println("iPad客户端");
		 }  else {
			 System.out.println("其他客户端");
			 bIsPc = true;
		 }
		 return bIsPc;
	}

}
